import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper';
import { App, ElementNamespace } from 'vue';

let app: App;
export default function <HostElement>(createApp: () => App<HostElement>,
    rootContainer: HostElement | string, isHydrate?: boolean, namespace?: boolean | ElementNamespace) {

    if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
        console.log(1232);
        app = createApp();
        app.mount(rootContainer, isHydrate, namespace);
    } else {
        renderWithQiankun({
            mount(props) {
                console.log('props', props)
                app = createApp();
                app.config.globalProperties.chenyf = '123';
                app.mount(props.container?.querySelector('#app') || 'root');

                // props.onGlobalStateChange((state, prev) => {
                //     // state: 变更后的状态; prev 变更前的状态
                //     console.log(321, state, prev);
                // }, true);

                // props.setGlobalState({ chenyf: 125 });

            },
            bootstrap() {
                console.log('vue app bootstrap');
            },
            update() {
                console.log('vue app update');
            },
            unmount() {
                console.log('vue app unmount');
                app?.unmount();
            }
        });
    }
}